
#include "GameState/Main/Main.h"
#include "Engine/Backbone.h"
#include "GameState/Main/PauseMenu.h"
#include "Gameplay/Play.h"
#include "Engine/Synaptic/Sound.h"
#include "GameState/DataStorage.h"

namespace GameState {

namespace Main {

Main::Main() :
	Sense("gameplay"),
	Reason("gameplay"),
	Visual("gameplay"),
	actualPlay(NULL),
	mainMenu(Misc::Frame(235,340,180,70), 4)
	{
	mainMenu[PAUSEMENU_MAINMENU] = new EntryMainMenu(Misc::Frame(235,300,180,35));
	mainMenu[PAUSEMENU_RESTART] = new EntryRestart(Misc::Frame(235,335,180,35));
	mainMenu[PAUSEMENU_QUIT] = new EntryQuit(Misc::Frame(235,370,180,35));
	mainMenu.active(true).hidden(false).focusAt(0).active(false).hidden(true);
	Engine::Synaptic::Sound::loadSound("sounds/menuChange.wav");
	Engine::Synaptic::Sound::loadSound("sounds/menuSelect.wav");
	hardMode = DataStorage<bool>::get("hardModeSetted");
}

Main::~Main() {
}

void Main::pulse(int delta) {
	levelName = DataStorage<std::string>::get("selectedLevel");
	bool hardModeSetted = DataStorage<bool>::get("hardModeSetted");
	if (levelName == "") {
		levelName = DataStorage<std::string>::get("defaultLevel");
	}
	std::string playingLevel = DataStorage<std::string>::get("playingLevel");
	if ((actualPlay == NULL) || actualPlay->isOver() || levelName != playingLevel || hardMode != hardModeSetted) {
		// TODO: level selection screen
		if (mainMenu.active()) mainMenu.active(false).hidden(true);
		if (actualPlay) delete actualPlay;
		DataStorage<std::string>::set("playingLevel", levelName);
		hardMode = hardModeSetted;
		if (DataStorage<bool>::get("hardModeSetted") == true)
			actualPlay = new Gameplay::Play(levelName, 20, 10, 25, 25);
		else actualPlay = new Gameplay::Play(levelName);
		actualPlay->setPaused(true);
	}
}

// Asks for generation of a graphical model to be displayed
Engine::Resource::GraphicList Main::display() {
	// this should display something?
	Engine::Resource::GraphicList gfxList;
	return gfxList;
}

void Main::keyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
	bool paused = false;
	Interface::Menu::MenuResponse res;
	switch (sym) {
		case SDLK_ESCAPE:
			paused = (actualPlay != NULL ? actualPlay->isPaused() : true);
			actualPlay->setPaused(!paused);
			mainMenu.hidden(paused).active(!paused);
			break;
		case SDLK_LEFT:
		case SDLK_UP:
			if (mainMenu.active()) {
				mainMenu.focusPrevious();
				Engine::Synaptic::Sound::play("sounds/menuChange.wav");
			}
			break;
		case SDLK_RIGHT:
		case SDLK_DOWN:
			if (mainMenu.active()) {
				mainMenu.focusNext();
				Engine::Synaptic::Sound::play("sounds/menuChange.wav");
			}
			
			break;
		case SDLK_RETURN:
			if (mainMenu.active()) {
				res = mainMenu.select();
				switch ((MenuIndex) res.index) {
					case PAUSEMENU_MAINMENU:
					case PAUSEMENU_RESTART:
						if (actualPlay) delete actualPlay;
						actualPlay = 0;
						mainMenu.active(false).hidden(true);
						break;
					default:
						break;
				}
				Engine::Synaptic::Sound::play("sounds/menuSelect.wav");
			}
			break;
		default:
			break;
	}

}

Main * Main::singleInstance; 

} // namespace GameState::Main

} // namespace GameState
